/**
 * slim slider
 * @author Levin
 * @version 1.0
 */
;(function($) {
    // Private functions.
    var p = {};
	p.M=function(dom,opts){
		this.opts=opts;
		dom=$(dom);
		this.ui={
			$ct:dom.find(opts.cssSlides),
			$items:dom.find(opts.cssSlide)
		};
		this.init();
	};
	p.M.prototype={
		init:function(){
			var me=this;
			this.cnt=this.ui.$items.length;
			if(this.cnt<2) return;
			
			this.curIdx=0;
			this.height=this.ui.$items.eq(0).outerHeight(true);
			this.width=this.ui.$items.eq(0).outerWidth(true);
			
			this.reverse=false;
			this.timer0=null;
			this.timer1=null;

			//events
			this.ui.$ct.hover(function(){
				me._reset();
			},function(){
				me.go();
			});

			me.go();
		},
		go:function(){
			var me=this;
			me._reset();
			me.timer1=window.setInterval(function(){
				me.step();
			},me.opts.speed);
		},
		step:function(){
			if(this.reverse){
				this.curIdx--;
			}else{
				this.curIdx++;
			};

			if(this.curIdx>=this.cnt){
				this.reverse=true;
				this.curIdx=this.cnt-2;
			}else if(this.curIdx<0){
				this.reverse=false;
				this.curIdx=1;
			};

			var opt_effect={};
			if(this.opts.vertical){
				opt_effect.top=(this.reverse?'+':'-')+'='+this.height;
			}else{
				opt_effect.left=(this.reverse?'+':'-')+'='+this.width;
			};

			this.ui.$ct.stop(true,true).animate(opt_effect,{
				duration:this.opts.duration,
				easing:this.opts.easing
			});

			return this;
		},
		_reset:function(){
			window.clearInterval(this.timer1);
			window.clearTimeout(this.timer0);
			return this;
		}
	};
    //main plugin body
    $.fn.slimSlider = function(opts) {
        // Set the options.
        opts = $.extend({}, $.fn.slimSlider.defaults, opts||{});
        // Go through the matched elements and return the jQuery object.
        return this.each(function() {
			new p.M(this,opts);
        });
    };
    // Public defaults.
    $.fn.slimSlider.defaults = {
		speed:5000,
		duration:'normal',
		easing:null,
		vertical:true,
		cssSlide:'.slide',
		cssSlides:'.slides'
    };
})(jQuery); 